package com.android.hedgehog;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;

import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Screen;
import com.android.hedgehog.framework.Input.TouchEvent;

public class Introduction extends Screen {
	public enum GameState {
		Greet, Name, Greet2, Age, WrongAge, Greet3, Finish
	}

	GameState state;
	Graphics graphics;
	float totalTime;
	float flicker;
	String intro = "Greetings Human Challenger. How are you today? what is the name that you go by?";
	boolean flickerOn;
	Message introduction;
	// Keyboard board;
	Keyboard2 board;
	float deltaTime;
	ArrayList<String> message = new ArrayList<String>();
	String userName;
	String age;
	int age2;
	Box enter;
	Box enter2;

	public Introduction(Game game) {
		super(game);
		graphics = game.getGraphics();
		introduction = new Message(graphics,intro);
		board = new Keyboard2(graphics);
		state = GameState.Greet;
		enter = new Box(graphics, "ENTER", 0, 100, 435, 120, 38);
		enter2 = new Box(graphics, "ENTER", 0, 100, 335, 120, 38);
	}

	@Override
	public void update(float deltaTime) {
		this.deltaTime = deltaTime;
		totalTime += deltaTime;

		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

		if (state == GameState.Greet)
			updateGreet(deltaTime);
		if (state == GameState.Name)
			updateName(touchEvents, deltaTime);
		if (state == GameState.Greet2)
			updateGreet2(deltaTime);
		if (state == GameState.Age)
			updateAge(touchEvents, deltaTime);

	}

	private void updateAge(List<TouchEvent> touchEvents, float deltaTime2) {
		if (totalTime > flicker) {
			flicker += .28f;
			flickerOn = !flickerOn;
		}

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) { // TOUCH UP
				if (enter2.click(event.x, event.y)) {
					state = GameState.Greet3;
					age = board.output;
					age2 = Integer.parseInt(age);
					if (age2 > 98 || age2 < 3) {
						introduction = new Message(graphics,age
								+ "... i don't believe you.  please, enter your real age");
						state = GameState.Greet2;
					} else {
						introduction = new Message(graphics,age + "? if you say so");
						state = GameState.Greet3;
					}
					return;
				}
				board.click(event.x, event.y);
			}
		}
	}

	private void updateGreet(float deltaTime) {
		if (introduction.finishedTyping) {
			state = GameState.Name;
		}
	}

	private void updateName(List<TouchEvent> touchEvents, float deltaTime) {
		if (totalTime > flicker) {
			flicker += .37f;
			flickerOn = !flickerOn;
		}

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) { // TOUCH UP
				if (enter.click(event.x, event.y)) {
					userName = board.output;
					if (userName.length() < 3) {
						introduction = new Message(graphics,"choose a longer name");
						state = GameState.Greet;
					} else {
						introduction = new Message(graphics,userName + ", How old are you?");
						state = GameState.Greet2;
					}
					return;
				}
				board.click(event.x, event.y);
			}
		}
	}

	private void updateGreet2(float deltaTime) {
		if (introduction.finishedTyping) {
			board.setMode(3, false, true, true);
			state = GameState.Age;

		}
	}

	@Override
	public void present(float deltaTime) {
		graphics.drawBackground(Color.BLACK);
		introduction.display(deltaTime);
		if (state == GameState.Greet)
			presentGreet();
		if (state == GameState.Name)
			presentName();
		if (state == GameState.Greet2)
			presentGreet2();
		if (state == GameState.Age)
			presentAge();
		if (state == GameState.Greet3)
			presentGreet3();

	}

	private void presentGreet3() {
	}

	private void presentAge() {
		if (flickerOn)
			graphics.drawUniversalText(">", 2, false, 5, 120, 20, 255, Color.GREEN);

		board.display();
		graphics.drawUniversalText("" + board.output, 2, false, 17, 120, 20, 255, Color.GREEN);

		enter2.draw();
	}

	private void presentGreet() {
	}

	private void presentName() {

		if (flickerOn)
			graphics.drawUniversalText(">", 2, false, 5, 120, 20, 255, Color.GREEN);

		graphics.drawUniversalText(board.output, 2, false, 17, 120, 20, 255, Color.GREEN);
		board.display();
		enter.draw();
	}

	private void presentGreet2() {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}

}
